﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework; // Xna Stuff
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content; // Input Reference we need to include here

/*
 * Rewritten Mouse Class, as i am new to this, i want to try it another
 * way, this way makes a bit more logical sence to me
 */
namespace LandBaron
{
    public class MouseController
    {
        /* Declare all the Vars we will need, we are going to be using get/set
         * This is a way different way to handle vars, but just as useful
         * and looks like you know what your doing, even though i dont ;p
         */
        // MouseStates
        MouseState previousMouseState, currentMouseState;
        Texture2D texture;

        // The mouses X Y
        //Private Vector2 used within this class, to set the value of a Public Vector2
        private Vector2 m_position;

        /// <summary>
        /// Returns or sets the Mouse X,Y
        /// </summary>
        public Vector2 Position
        {
            get { return m_position; } // When we ask for this value its going to give us the value of m_position
            set { m_position = value; } // When we set this value, its sets the value of m_position
        }

        // Private Rectangle used to set the value of our Public Rectangle
        private Rectangle m_rectangle;

        /// <summary>
        /// Returns or sets the value of our Mouse Rectangle
        /// </summary>
        public Rectangle Rectangle
        {
            get { return m_rectangle; }
            set { m_rectangle = value; }
        }

        // Way to Check for Clicks
        
        /// <summary>
        /// Returns true if Mouse is LeftClicked
        /// </summary>
        public bool LeftClick
        {
            get {return currentMouseState.LeftButton == ButtonState.Pressed;} 
            // We dont use a set here, as we only want it to return the state, not to forcefully set it.
        }

        /// <summary>
        /// Returns true if Mouse is RightClicked
        /// </summary>
        public bool RightClick
        {
            get { return currentMouseState.RightButton == ButtonState.Pressed; }
        }

        /// <summary>
        /// Returns true if the mouse was LeftClicked for the first time
        /// </summary>
        public bool NewLeftClick
        {
            get
            {
                return currentMouseState.LeftButton == ButtonState.Pressed &&
                    previousMouseState.LeftButton == ButtonState.Released;
            }
        }

        /// <summary>
        /// Returns true if the Mouse was RightClicked for the first time
        /// </summary>
        public bool NewRightClick
        {
            get
            {
                return currentMouseState.RightButton == ButtonState.Pressed &&
                    previousMouseState.RightButton == ButtonState.Released;
            }
        }

        /// <summary>
        /// Returns true if the Mouse LeftButton was pressed, but now is not
        /// </summary>
        public bool LeftRelease
        {
            get { return !LeftClick && previousMouseState.LeftButton == ButtonState.Pressed; }
        }

        /// <summary>
        /// Returns true if the Mouse RightButton was pressed, but now is not
        /// </summary>
        public bool RightRelease
        {
            get { return !RightClick && previousMouseState.RightButton == ButtonState.Pressed; }
        }


        /* Ok now we move on to the constructors i talked about in the last class
         * These are just different ways to pass information to this class
         */

        // Blank Constructor
        /// <summary>
        /// Empty Mouse Object
        /// </summary>
        public MouseController()
        {
        }
        /// <summary>
        /// Mouse Object with the icon we want
        /// </summary>
        /// <param name="texture"></param>
        public MouseController(Texture2D texture)
        {
            this.texture = texture;
        }
        /// <summary>
        /// Creates a new Mouse object, and loads the texture for it into memory
        /// </summary>
        /// <param name="content">The Games Content Manager</param>
        /// <param name="assetName">the texture name f our mouse texture</param>
        public MouseController(ContentManager content, string assetName)
        {
            texture = content.Load<Texture2D>(assetName);
        }
        // This is the end of our constructors
        /// <summary>
        /// Gets the current mousestate, and changed mouse Position and the Rectangle its using each frame
        /// </summary>
        public void Update()
        {
            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();

            Position = new Vector2(currentMouseState.X, currentMouseState.Y);

            Rectangle = new Rectangle((int)Position.X, (int)Position.Y, texture.Width, texture.Height);
        }

        /// <summary>
        /// Draws our Mouse object to the screen
        /// </summary>
        /// <param name="spriteBatch">The SpriteBatch used to draw this object</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            // Make sure we have a texture
            if (texture != null)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(texture, Position, Color.White);
                spriteBatch.End();
            }
        }

    }
}
